<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	<title>notes</title>
	<style type="text/css">
	  table
	  {
	  border-collapse: collapse;
	  }
	  table, tr, th, td
	  {
	  border: 1px solid black;
	  padding-left: 8px;
	  padding-right: 8px;
	  }
	  th, td
	  {
	  min-width: 2em;
	  }
	  pre
	  {
	  border-style: solid
	  }
	  .comment
	  {
	  color: green;
	  }
	</style>
	<link rel="stylesheet" title="plain" type="text/css" href="plain.css" />
	<link rel="alternate stylesheet" title="colour" type="text/css" href="colour.css"/>
</head>
<body>
	<h2>Notes</h2>
	<p>Plan for this class:<ol>
		<li><h3>Review of functions and pointers:</h3><ul>
		    <li>recap of Wednesday:<ul>
			<li>using pointers / references to make code re-usable</li>
                <li>pointer / reference use from 3 viewpoints</li>
		    </ul></li>
		    <li>what was difficult?</li>
		    <li>running out of names</li>
		    <li>repeating the same concept in different contexts</li>
		    <li>the output of reasonable English sentences</li>
		</ul></li>
		<li><h3>Object oriented programming:</h3><ul>
		    <li>not a language</li>
		    <li>C++ not an object oriented language</li>
		    <li>evolution of abstraction</li>
		    <li>new perspective on programming</li>
		    <li>simulation (Simula67)</li>
		    <li>programming through interacting behaviours</li>
		    <li>why is this a good fit for games?<ul>
			<li>take another look at procedural combat loop</li>
			<li>describe how it works at the highest level</li>
			<li>how about scaling up?</li>
		    </ul></li>
		    <li>it's not all roses...<ul>
			<li>dumbing down</li>
			<li>performance</li>
			<li>rigidity</li>
		    </ul></li>
		</ul></li>
		<li><h3>Principles of object oriented programming</h3><ul>
		    <li>abstraction</li>
		    <li>encapsulation (information hiding)</li>
		    <li>inheritance</li>
		    <li>polymorphism</li>
		    <li>message passing</li>
		    <li>tell, don't ask</li>
		    <li>note: "getters" are asking</li>
		    <li>shallow hierarchies</li>
		    <li>ABC (Abstract Base Class)</li>
		    <li>use IS-A for interface</li>
		    <li>use HAS-A for combinations</li>
		</ul></li>
		<li><h3>Exercise: identify objects</h3><ul>
		    <li>take any of the code we've worked on so far</li>
		    <li>look for good candidates to make into objects</li>
		    <li>draw out your objects on paper</li>
		    <li>consider the messages going between the objects</li>
		    <li>remember to tell rather than ask</li>
		</ul></li>
		<li><h3>Object oriented features of C++</h3><ul>
		    <li>class keyword</li>
		    <li>member functions</li>
		    <li>with structures and functions:</li>
		    <pre>

struct character {

     int initiative_modifier;
};

int roll_initiative(character * combatant)
{
     return roll(20) + combatant-&gt;initiative_modifier;
}


     ...
     if (roll_initiative(&amp;player) &gt; roll_initiative(enemy)) {
     ...     
		    </pre>
		    <li>with classes and member functions:</li>
		    <pre>

class character {

     int initiative_modifier;
public:
     int roll_initiative() { return roll(20) + initiative_modifier; }
};


     ...
     if (player.roll_initiative() &gt; enemy-&gt;roll_initiative()) {
     ...
		    </pre>
		    <li>an alternative layout:</li>
		    <pre>

class character {

     int initiative_modifier;
public:
     int roll_initiative();
};

int character::roll_initiative()
{
     return roll(20) + initiative_modifier;
}
		    </pre>
		    <li>public, private, and protected data and functions</li>
		    <li>friends</li>
		    <li>constructors and destructors</li>
		    <li>side-effect: scope</li>
		    <pre>

class auto_timer {

     time_t start;
public:
     auto_timer(): start(time(0)) {
     }
     ~auto_timer() {

          cout &lt;&lt; "time taken: " &lt;&lt; time(0) - start &lt;&lt; endl;
     }
};

     ...
     {
          auto_timer t;
          ...
     }
		    </pre>
		    <li>RAII (resource acquisition is initialisation)</li>
		    <li>useful when code can cause exceptions</li>
		    <pre>
     {
          FILE * file = fopen("levels.txt");
          if (file) {

               ...
               // some exception happens here...
               ...
               fclose(file);
          }


     ...
     {
          fstream file("levels.txt");
          if (file.is_open()) {
          
               ...
               // some exception happens here...
               ...
          }
     }
		    </pre>
		    <li>new and delete</li>
		    <li>"smart" pointers</li>
		    <li>the base class</li>
		    <li>pure virtual function</li>
		    <li>abstract base class</li>
		    <li>pure virtual destructor</li>
		    <li>pointer to ABC</li>
		    <pre>

class character {

protected:
     virtual int initiative_modifier() = 0;

public:
     virtual ~character() = 0;
     int roll_initiative() { return roll(20) + initiative_modifier(); }
};


class PC: public character {

     int dexterity;

protected:
     virtual int initiative_modifier() { return (dexterity - 10) / 2; }

public:
     virtual ~PC() {}
};


class monster: public character {

     int initiative_modifier;

protected:
     virtual int initiative_modifier() { return initiative_modifier; }

public:
     virtual ~monster() {}
};


     ...
     character * player = new PC;
     character * dragon = new monster;
     ...
     if (player-&gt;roll_initiative() &gt; dragon-&gt;roll_initiative()) {

          ...
     }
     ...
     delete player;
     delete dragon;
     ...
		    </pre>
		    <li>object type info</li>
		    <li>the vtbl (virtual function pointer table)</li>
		    <li>how virtual functions work</li>
		    <li>draw out type info data and vtbl</li>
		    <li>discuss performance issues</li>
		    <li>the dynamic cast</li>
		    <pre>

     ...
     monster * dragon = dynamic_cast&lt;monster*&gt;(enemy);
     if (dragon) {
     
          ...
     }
		    </pre>

		    <li>const modifier</li>
		    <pre>

class PC: public character {

public:
     virtual int roll_initiative() const;
};
		    </pre>
		    <li>mutable modifier</li>
		</ul></li>
		<li><h3>Exercise: write or re-write some code</h3><ul>
		    <li>take any existing procedural code</li>
		    <li>design and define the classes</li>
		    <li>create object instances and bend them to your will</li>
		    <li>tip: it's often easier to work backwards:<ul>
			<li>write the calling code first</li>
			<li>then define the classes</li>
		    </ul></li>
		</ul></li>
	  </ol>
	</p>
</body>
</html>
